#!/usr/bin/sh

set -euo pipefail

hashDir(){
  (find $1 -type f -print0  | sort -z | xargs -0 grep -v 'createdAt' |sha1sum;
   find $1 \( -type f -o -type d \) -print0 | sort -z | xargs -0 stat -c '%n %a') | sha1sum
}

rc=0

bundleHash=$(hashDir ./bundle)
make bundle
if [ "$(hashDir ./bundle)" != "$bundleHash" ] ; then
  echo ""
  echo "A change was found in the ./bundle that was not generated using the config."
  echo "Please run make bundle and commit the results"
  echo ""
fi

set +e
fitCheck=$(grep -nEIr --include=*_test.go 'FContext|FEntry|FDescribe|FDescribeTable|FIt')
if [ "$fitCheck" != "" ]; then
    rc=1
    echo "Focused tests were found in the code base"
    echo "Please remove any of FIt, FEntry, FDescribeTable, etc. from:"
    echo ""
    echo "$fitCheck"
    echo ""
fi

check=$(grep -nEIr --include=*.go --include=*.toml 'type.*"lua"')
if [ "$check" != "" ]; then
    rc=1
    echo "Transform found that depends upon lua."
    echo "Please convert transform to use VRL instead"
    echo ""
    echo "$check"
    echo ""
fi
set -e

exit $rc
